package com.ruoyi.glsims.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.glsims.domain.Prove;
import com.ruoyi.glsims.service.IProveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 离校申请Controller
 *
 * @author airted
 * @date 2023-09-12
 */
@RestController
    @RequestMapping("/gradu_info/leave_sup_manager")
public class ProveController extends BaseController
{
    @Autowired
    private IProveService proveService;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * 查询离校申请列表（学生和老师的情况不同）
     */
//    @PreAuthorize("@ss.hasPermi('gradu_info:leave_sup_manager:list')")
    @PreAuthorize("@ss.hasAnyRoles('admin,teacher,student')")
    @GetMapping("/list")
    public TableDataInfo list(Prove prove)
    {
        startPage();

        Long uid = SecurityUtils.getUserId();
        String str = "";

        Long roleId = userRoleMapper.selectRoleIdByUserId(uid);
        str = roleMapper.selectRoleById(roleId).getRoleKey();

        List<Prove> list = null;
        if (str.equals("teacher") || str.equals("admin")) {
            list = proveService.selectProveList(prove);
        } else {
            prove.setUserId(uid);
            list = proveService.findProveListFromStudent(prove);
        }
        return getDataTable(list);
    }


    /**
     * 导出离校申请列表
     */
//    @PreAuthorize("@ss.hasPermi('gradu_info:leave_sup_manager:export')")
    @PreAuthorize("@ss.hasAnyRoles('admin,teacher,student')")
    @Log(title = "离校申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Prove prove)
    {
        List<Prove> list = proveService.selectProveList(prove);
        ExcelUtil<Prove> util = new ExcelUtil<Prove>(Prove.class);
        util.exportExcel(response, list, "离校申请数据");
    }

    /**
     * 获取离校申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gradu_info:leave_sup_manager:query')")
    @PreAuthorize("@ss.hasAnyRoles('admin,teacher,student')")
    @GetMapping(value = "/{proveId}")
    public AjaxResult getInfo(@PathVariable("proveId") Long proveId)
    {
        return success(proveService.selectProveByProveId(proveId));
    }

    /**
     * 新增离校申请
     */
    @PreAuthorize("@ss.hasPermi('gradu_info:leave_sup_manager:add')")
//    @PreAuthorize("@ss.hasAnyRoles('admin,student')")
    @Log(title = "离校申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prove prove)
    {
        return toAjax(proveService.insertProve(prove));
    }

    /**
     * 修改离校申请
     */
//    @PreAuthorize("@ss.hasPermi('gradu_info:leave_sup_manager:edit')")
    @PreAuthorize("@ss.hasAnyRoles('admin,teacher,student')")
    @Log(title = "离校申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prove prove)
    {
        return toAjax(proveService.updateProve(prove));
    }

    /**
     * 删除离校申请
     */
    @PreAuthorize("@ss.hasPermi('gradu_info:leave_sup_manager:remove')")
//    @PreAuthorize("@ss.hasAnyRoles('admin,teacher,student')")
    @Log(title = "离校申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{proveIds}")
    public AjaxResult remove(@PathVariable Long[] proveIds)
    {
        return toAjax(proveService.deleteProveByProveIds(proveIds));
    }
}
